/**
 * 表单设计----组件绑定字段
 */
mini.parse();
var grid = mini.get("datagrid1");

var url;
var param = {};
var click_type;
function SetData(data) {
    var data = mini.clone(data);
    var selected = data.selected;
    var click_type = data.click_type;
    var keyList_main = data.keyList_main;
    var keyList_child = data.keyList_child;
    var isSelectMain;//true:主表已被选择
    var flag;//true:显示主表

    if(data.click_type == 'table' || data.click_type == 'tab_table') {//子表
        flag = false;
        if(data.key == '') {
        	isSelectMain = false;
            mini.confirm("请先选择主表", "确定",
                function (action) {
                    onCancel();
                }
            );
            
        }else {
            url = "/tableClass/findByMTableToSTable.do";
            param = {
                tableClassId: data.key_id
            };
            isSelectMain = true;
        }

    }else {//主表
        url = "/tableClass/list.do";
//    	url = "/findBymTableAndNO.do";
        isSelectMain = true;
        flag = true;
    }
    //加载表格
    if(isSelectMain){
    	 $.ajax({
    	        url: ajaxUrl + url,
    	        type: "post",
    	        dataType: "json",
    	        data : param,
    	        success: function(grid_data) {
    	            // console.log(grid_data);
    	            grid.loading();
//    	            grid.collapseAll();
    	            var mainTreeData = dbTree(grid,grid_data.data);//获取主表树

                    console.log(keyList_main);
    	            grid.on("beforeselect",function(e){
    	                console.log(e.record);

                        if(e.record.PID == -1) {//表名阻止选中
                            e.cancel=true;
                        }
    	                for(var i=0;i<selected.length;i++) {
    	                    //字段被选择过
    	                    if(selected[i].cTableField == e.record.field &&
                                selected[i].cTableName == e.record.tableClassName) {
                                e.cancel=true;     //阻止选中
                                mini.alert("该字段已被绑定，请重新选择！");
                            }
                        }
    	               
                        // if(click_type.indexOf("datepicker")>-1) {
                        //     //时间组件不能绑定时间类型以外的字段
                        //     if(e.record.type != 'datetime') {
                        //         e.cancel=true;     //阻止选中
                        //         mini.alert("字段类型不匹配，请重新选择！");
                        //     }
                        // }else if(click_type.indexOf("spinner")>-1) {
                        //     //数值组件不能绑定数值类型以外的字段
                        //     if(e.record.type.indexOf('char')>-1 ||e.record.type == 'text' || e.record.type == 'datetime') {
                        //         e.cancel=true;     //阻止选中
                        //         mini.alert("字段类型不匹配，请重新选择！");
                        //     }
                        // }else {
                        //     //文本类
                        //     if(e.record.type.indexOf('char') == -1) {
                        //         e.cancel=true;     //阻止选中
                        //         mini.alert("字段类型不匹配，请重新选择！");
                        //     }
                        // }

    	                if(flag) {
                            // var filterNull = keyList_main.filter(d=>d);//筛选空格以及无效字段
                            if(keyList_main[0] != "" && keyList_main.length>1 && keyList_main.indexOf(e.record.tableClassName)) {
                                e.cancel=true;
                                mini.alert("只能选择同一张主表的字段！");
                            }
                        }else {
    	                    // console.log(keyList_child)
                            // if(keyList_child[0] != "" && keyList_child.filter(d=>d).length>1 && keyList_child.indexOf(e.record.tableClassName)) {
                            //     e.cancel=true;
                            //     mini.alert("只能选择同一张子表的字段！");
                            // }
                        }
    	            })
    	        },
    	        error : function(data){
    	            // alert('获取失败');
    	        }
    	    });
    }
   
}

//确定按钮之前
function onTableOk() {
    var grid = mini.get("datagrid1");
    var gridSelected = grid.getSelectedNode();
    // console.log(gridSelected);
    if(gridSelected == undefined) {
        mini.alert('请选择字段！');
    }else {
        return gridSelected; //返回选中的数据给父组件
    }
}

//确定按钮
function onOk(e) {
    CloseWindow("ok");
}
//取消按钮
function onCancel(e) {
    CloseWindow("cancel");
}

function CloseWindow(action) {

    if (window.CloseOwnerWindow)
        return window.CloseOwnerWindow(action);
    else
        window.close();
}

//表格树数据加载
var dbData = [];//存放转换后的数据库表数据
function dbTree(grid,data) {
    dbData = [];
    console.log(data,'data');
    for(var i=0;i<data.length;i++) {
        dbData[i] = {
            tableClassName: data[i].tableClassName,
            tableChClassName: data[i].tableChClassName,
            cTableName: data[i].tableChClassName,
            field: '',
            fieldCh: '',
            tableClassId: data[i].tableClassId + '',//id
            PID: -1,
            checked: false,
            expanded: false,
            children:[]
        };
        for(var j=0;j<data[i].createTables.length;j++) {
            var c = data[i].createTables[j];
            dbData[i].children.push({
                // cTableField: c.field,
                cTableName: data[i].tableChClassName,
                tableClassName: data[i].tableClassName,
                // formListId: data[i].formListId,
                type: c.type,
                field: c.field,
                fieldCh: c.fieldCh,
                tableClassId: c.tableId + '',//id
                PID: data[i].tableClassId + '',
                expanded: false,
                checked: false
            });
        }
    }
    console.log(dbData,'dbData');
    grid.setData(dbData);
    return dbData;
}